/*
	Used for reading ("GMK") files produced from Game Maker 8.0
*/

#include "gameData.h"
#include "dataStream.h"
#include <stdio.h>
#include <stdarg.h>

#define GMK80MAGICNUMBER 1234321
#define VERSIONPARSE 800
enum PARSESTATUS {
	SUCCESS, //Succesfull parse
	INVALID_FILE, //The file specified does not appear to be a GMK(8.0)
	UNKNOWN_ERROR //Unknown error has occured
};

class GMK80Reader {
private:
	gameData*		dataContainer; //The structure that the data will be written to (eg the "gameData" what we are reading the data into)
	dataStream*		fileData; //Stream containing the data to parse
	bool			verbose; //Verbose logging? If set to true, parsing operations will generate a log of what's happening

	void log(const char* format, ...); //Output a message

	bool validateFile(); //Validates the file to make sure we can parse it.

	//The following functions assume that we are already seeked to the beginning of the data (eg. we are already seeked to the beginning of the sprites block)
	void parseGameSettings(dataStream *data);
	void parseGameInformation(dataStream *data);
	void parseIncludedFile(dataStream *data);
	void parseTrigger(dataStream *data);
	void parseSound(dataStream *data);
	void parseSprite(dataStream *data);
	void parseBackground(dataStream *data);
	void parsePath(dataStream *data);
	void parseScript(dataStream *data);
	void parseFont(dataStream *data);
	void parseTimeline(dataStream *data);
	void parseObject(dataStream *data);
	void parseRoom(dataStream *data);
	void RecursiveTreeAdd(ResourceTreeDef* Node, int childrenCount, dataStream * data);
	

public:
	GMK80Reader(gameData* dataStructure);
	PARSESTATUS parseData(dataStream* stream, bool verbose = false); //Parse the specified stream.
	PARSESTATUS parseData(std::string fileName, bool verbose = false); //Overload, allows file name specification, and stream will be created automatically
};